//
//  FocusInfoModel.m
//  govern
//
//  Created by LiangLigeng on 14-8-22.
//  Copyright (c) 2014年 LiangLigeng. All rights reserved.
//

#import "FocusInfoModel.h"
#import "FMDatabase.h"
#import "FMResultSet.h"

@implementation FocusInfoModel


@synthesize title,ico,createdate,turnUrl,type,category,status,ordernum;

-(NSDictionary *)toDictionary
{
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"title",@"title",@"ico",@"ico",@"createdate",@"createdate",@"turnUrl",@"turnUrl",@"type",@"type",@"category",@"category",@"status",@"status",@"ordernum",@"ordernum",nil];
    return dic;
}

+(FocusInfoModel *)focusFromDictionary:(NSDictionary *)dic
{
    FocusInfoModel *focus=[[FocusInfoModel alloc] init];
    [focus setTitle:[dic objectForKey:@"title"]];
    [focus setIco:[dic objectForKey:@"ico"]];
    [focus setCreatedate:[dic objectForKey:@"createdate"]];
    [focus setTurnUrl:[dic objectForKey:@"turnUrl"]];
    [focus setType:[dic objectForKey:@"type"]];
    [focus setCategory:[dic objectForKey:@"category"]];
    [focus setStatus:[dic objectForKey:@"status"]];
    [focus setOrdernum:[dic objectForKey:@"ordernum"]];
    return focus;
}

+(BOOL)saveFocus:(FocusInfoModel *)focusModel
{
    FMDatabase *db=[FMDatabase databaseWithPath:DATABASE_PATH];
    if (![db open]) {
        NSLog(@"数据库打开失败");
        return NO;
    }
    
    [FocusInfoModel checkTableCreateInDb:db];
    
    NSString *sql=@"insert or replace into focusinfo('title','ico','createdate','turnUrl','type','category','status','ordernum') values(?,?,?,?,?,?,?,?)";
    BOOL worked=[db executeUpdate:sql,focusModel.title,focusModel.ico,focusModel.createdate,focusModel.turnUrl,focusModel.category,focusModel.status,focusModel.ordernum];
    [db close];
    
    return worked;
}

+(BOOL)checkTableCreateInDb:(FMDatabase *)db
{
    NSString *sql=@"CREATE  TABLE  IF NOT EXISTS focusinfo ('title'  TEXT, 'ico' TEXT, 'createdate' TEXT, 'turnUrl' TEXT, 'type' TEXT, 'category' TEXT, 'status' TEXT, 'ordernum' TEXT)";
    FMResultSet *s = [db executeQuery:@"SELECT COUNT(*) FROM focusinfo"];
    int totalCount = 0;
    if ([s next]) {
        totalCount = [s intForColumnIndex:0];
    }
    BOOL worked=[db executeUpdate:sql];
    if (worked&&totalCount<1) {
        /*NSString *insertStr=@"INSERT INTO 'focusinfo' VALUES ('全国要闻', '2130837567', '0', '495c5f47-816b-470a-9637-a7158938c4e9', 'id', '1', '0', '2014-08-11 16:23:27')";*/
        NSString *insertStr=@"INSERT INTO 'focusinfo' VALUES ('全国要闻', '2130837567', '0', '495c5f47-816b-470a-9637-a7158938c4e9', 'id', '1', '0', '2014-08-11 16:23:27'),('政府领导', '2130837594', '1', 'd84aa457-4c1b-47af-b13c-8b0aa84429a4', 'id', '1', '0', '2014-08-11 16:23:27'),('婚育收养', '2130837551', '1', 'ef051577-a346-4400-842b-4566ed08ad79', 'id', '1', '0', '2014-08-11 16:23:27'),('社保查询', '2130837575', '0', 'http://gzgy.lss.gov.cn/gyldbzw/72902018968059904/', 'url', '1', '0', '2014-08-11 16:23:27'),('基本省情', '2130837552', '0', 'e8109f86-60d6-492d-ba46-f68148bcb90a', 'id', '0', '0', '2014-08-11 16:27:45'),('贵州要闻', '2130837548', '0', 'f917e766-44b9-43c9-854a-55a7e2e92095', 'id', '0', '0', '2014-08-11 16:30:13'),('政府采购', '2130837593', '1', 'http://www.gzgov.gov.cn/xxgk/zfcg/index.shtml', 'url', '0', '0', '2014-08-11 20:41:10'),('部门动态', '2130837538', '0', 'f619f3c7-e461-4b07-8624-fe1d467e8718', 'id', '0', '0', '2014-08-20 07:37:25'),('领导信箱', '2130837561', '1', 'LeadingMailActivity.class', 'activity', '0', '0', '2014-08-21 23:22:47'),('人民网网民留言', '2130837573', '0', 'http://liuyan.people.com.cn/index.php?gid=35', 'url', '0', '0', '2014-08-21 23:22:49');";
        [db executeUpdate:insertStr];
    }
    
    
    
    return worked;
}

+(BOOL)deleteFocusByTitle:(NSString *)title
{
    FMDatabase *db=[FMDatabase databaseWithPath:DATABASE_PATH];
    if (![db open]) {
        NSLog(@"打开数据库失败");
        return NO;
    }
    
    [FocusInfoModel checkTableCreateInDb:db];
    
    NSString *sql=@"delete focusinfo where title=?";
    BOOL worked=[db executeUpdate:sql,title];
    [db close];
    
    return  worked;
}

+(NSMutableArray *)getAllFocus
{
    FMDatabase *db=[FMDatabase databaseWithPath:DATABASE_PATH];
    if (![db open]) {
        NSLog(@"打开数据库失败");
        return NO;
    }
    
    [FocusInfoModel checkTableCreateInDb:db];
    
    NSMutableArray *arr=[[NSMutableArray alloc]init];
    
    NSString *sql=@"select * from focusinfo";
    FMResultSet *result=[db executeQuery:sql];
    FocusInfoModel *model;
    while ([result next]) {
        model=[[FocusInfoModel alloc]init];
        model.title=[result stringForColumn:@"title"];
        model.ico=[result stringForColumn:@"ico"];
        model.createdate=[result stringForColumn:@"createdate"];
        model.turnUrl=[result stringForColumn:@"turnUrl"];
        model.type=[result stringForColumn:@"type"];
        model.category=[result stringForColumn:@"category"];
        model.status=[result stringForColumn:@"status"];
        model.ordernum=[result stringForColumn:@"ordernum"];
        [arr addObject:model];
    }
    [db close];
    
    return arr;
}

@end
